﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Checkout : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack) return;
        if (Session["Username"] == null)
            Server.Transfer("Login.aspx");

        if (Session["number"] == null || Session["number"].ToString() == "0")
            Server.Transfer("Login.aspx");

        string cart = Session["cart"].ToString();

        ItemBLL it = new ItemBLL();
        CartCheckout.DataSource = it.getItemInCart(cart);
        CartCheckout.DataBind();

        UserBLL us = new UserBLL();
        us = us.getUserInfo(Session["Username"].ToString());
        lbName.Text = us.name;
        lbAddress.Text = us.address;
        lbEmail.Text = us.email;
        lbPhone.Text = us.phone;

        txtName.Text = lbName.Text;
        txtAddress.Text = lbAddress.Text;
        txtPhoneNumber.Text = lbPhone.Text;
        txtEmail.Text = lbEmail.Text;
    }
    protected int getTotalCash()
    {
        int totalCash = 0;
        string[] cart = Session["cart"].ToString().Split(',');
        ItemBLL it = new ItemBLL();

        int num = 0;
        for (int i = 0; i < cart.Length - 1; i++)
        {
            num = Int32.Parse(Session["item" + cart[i]].ToString());
            totalCash += it.getCash(cart[i]) * num;
        }
        return totalCash;
    }
    protected int getSumCash(string id)
    {
        int num = Int32.Parse(Session["item" + id].ToString());
        ItemBLL it = new ItemBLL();
        return (it.getCash(id) * num);
    }
    protected void btnDatHang_Click(object sender, EventArgs e)
    {
        if (Session["Username"] == null)
            Response.Redirect("Default.aspx");
        else
        {
            UserBLL us = new UserBLL();
            us = us.getUserInfo(Session["Username"].ToString());

            CartsBLL cart = new CartsBLL();
            cart.name = txtName.Text;
            cart.email = txtEmail.Text;
            cart.phone_number = txtPhoneNumber.Text;
            cart.address = txtAddress.Text;
            cart.user_id = us.id;
            cart.cash_sum = getTotalCash();
            cart.payment = int.Parse(ComboListPayment.SelectedIndex.ToString());

            if (cart.insertToCart(cart))
            {
                string[] temp = Session["cart"].ToString().Split(',');
                Cart_itemBLL cart_item = new Cart_itemBLL();
                int cartId = cart.getMaxCartId();

                for (int i = 0; i < temp.Length - 1; i++)
                {
                    cart_item.item_id = Int32.Parse(temp[i]);
                    cart_item.quantity = Int32.Parse(Session["item" + temp[i]].ToString());
                    cart_item.cash_total = getSumCash(temp[i]);
                    cart.insertCartDetail(cart_item, cartId);
                }

                Session["number"] = 0;
                for (int i = 0; i < temp.Length - 1; i++)
                {
                    Session["item" + temp[i]] = null;
                }
                Session["cart"] = null;
            }

            Response.Write("<script>alert('Đơn đặt hàng đã được lưu vào hệ thống!!! Chúng tôi sẽ giao hàng đến quý khách sớm nhất có thể!!!');</script>");
            Server.Transfer("Default.aspx");
        }
    }

    protected void btnCopy_Click(object sender, EventArgs e)
    {
        txtName.Text = "";
        txtAddress.Text = "";
        txtPhoneNumber.Text = "";
        txtEmail.Text = "";
    }
}